home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / vfork.c < prev    next >
C/C++ Source or Header  |  1995-12-31  |  21KB  |  687 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: vfork.c,v 1.9 1994/06/19 15:18:29 rluebbert Exp $
  21.  *
  22.  *  $Log: vfork.c,v $
  23.  *  Revision 1.9  1994/06/19  15:18:29  rluebbert
  24.  *  *** empty log message ***
  25.  *
  26.  *  Revision 1.7  1992/10/20  16:29:49  mwild
  27.  *  allow a vfork'd process to use the parents memory pool. The new function
  28.  *  vfork2() continues to use the old semantics.
  29.  *
  30.  *  Revision 1.6  1992/09/14  01:48:11  mwild
  31.  *  move kmalloc() out of Forbid() (since the allocator is now Semaphore-based).
  32.  *  move errno assignment after sigsetmask (thanks Niklas!)
  33.  *  remove dead code
  34.  *
  35.  *  Revision 1.5  1992/08/09  21:01:43  amiga
  36.  *  change to 2.x header files
  37.  *  duplicate calling stack frame in vfork_resume() instead of just doing rts.
  38.  *  temporary abort calling 1.3 vfork, until that's fixed again (when???).
  39.  *
  40.  *  Revision 1.4  1992/07/04  19:24:12  mwild
  41.  *  get passing of environment right.
  42.  *  change ix_sleep() calls to new semantics.
  43.  *
  44.  * Revision 1.3  1992/05/18  12:26:25  mwild
  45.  * fixed bad typo that didn't close files before sending wait message.
  46.  * Set childs Input()/Output() to NIL:, we only keep the files in our
  47.  * own filetable.
  48.  *
  49.  * Revision 1.2  1992/05/18  01:02:31  mwild
  50.  * add temporary Delay(100) before CloseLibrary() in the child after
  51.  * vfork(), there seem to arrive some late packets (don't know why..)
  52.  * pass NIL: filehandles as Input()/Output() to the child, so that the
  53.  * real I/O-handles only depend on ix-filetable for usage-count
  54.  *
  55.  * Revision 1.1  1992/05/14  19:55:40  mwild
  56.  * Initial revision
  57.  *
  58.  */
  59.  
  60. #define KERNEL
  61. #include "ixemul.h"
  62. #include "kprintf.h"
  63.  
  64. #include <sys/syscall.h>
  65. #include <sys/resource.h>
  66. #include <sys/wait.h>
  67. #include <stddef.h>
  68. #include <setjmp.h>
  69. #include <string.h>
  70. #include <stdlib.h>
  71.  
  72. #include <utility/tagitem.h>
  73. #include <dos/dostags.h>
  74.  
  75. #include "version.h"
  76.  
  77. void vfork_own_malloc ();
  78. void volatile vfork_longjmp (jmp_buf, int);
  79. extern char **dupvec (char **);
  80. void ruadd(struct rusage *ru, struct rusage *ru2);
  81.  
  82. /* having it in a struct makes parameter passing easier */
  83.  
  84. struct reg_parms {
  85.   jmp_buf jb;
  86. };
  87.  
  88. struct vfork_msg {
  89.   struct Message     vm_msg;
  90.   struct Process    *vm_self;    /* for validation purposes (1.3, grrr) */
  91.   struct Process     *vm_pptr;
  92.   struct reg_parms     *vm_regs;    /* parents context to restore */
  93.   int            vm_own_malloc;
  94.   int             vm_rc;        /* 0 if the child started normally, else errno */
  95. };
  96.  
  97. struct death_msg {
  98.   struct MinNode    dm_node;
  99.   struct Process    *dm_child;
  100.   int            dm_pgrp;
  101.   int            dm_status;
  102.   struct rusage        dm_rusage;
  103. };
  104.  
  105. /* this is the new process generated by vfork () ! */
  106. static void
  107. launcher (void)
  108. {
  109.   void *ixb = OpenLibrary ("ixemul.library", IX_VERSION);
  110.   struct Process *me = (struct Process *) FindTask (0);
  111.   struct vfork_msg *vm;
  112.   int omask;
  113.  
  114.   /* the launcher() generated by Arp's ASyncRun seems to get a weird
  115.      message. Verify that the message at least looks right.. (damn 1.3 support..) */
  116.   vm = 0;
  117.   do
  118.     {
  119.       if (! vm) 
  120.         WaitPort (& me->pr_MsgPort);
  121.       vm = (struct vfork_msg *) GetMsg (& me->pr_MsgPort);
  122.     }
  123.   while (vm->vm_self != me);
  124.       
  125.  
  126.   if (ixb)
  127.     {
  128.       /* get parents user area */
  129.       volatile struct user *pu = (struct user *) (vm->vm_pptr->pr_Task.tc_TrapData);
  130.       /* `my' user area. This way we don't have to recalculate it too often */
  131.       volatile struct user *mu = &u;
  132.       /* reaping the dup function of execve() ;-)) */
  133.       int fd, rc;
  134.  
  135.       /* link ourselves into the parents process lists. Guarantee single
  136.        * threaded access to those lists by locking out any other users of
  137.        * the library (nicer than to just call Forbid()) */
  138.       ix_lock_base ();
  139.       
  140.       /* our older sybling is the last recently created child of the parent */
  141.       mu->p_osptr = pu->p_cptr;
  142.       /* we have no younger sybling */
  143.       mu->p_ysptr = 0;
  144.       /* if we have an older sybling, point its `younger sybling' field at us */
  145.       if (mu->p_osptr)
  146.         {
  147.           struct user *ou = (struct user *) (mu->p_osptr->pr_Task.tc_TrapData);
  148.       ou->p_ysptr = me;
  149.     }
  150.       /* set the parents `last recently created child' field at us */
  151.       pu->p_cptr = me;
  152.  
  153.       /* inherit the process group of our parent */
  154.       mu->p_pgrp = pu->p_pgrp;
  155.       mu->p_pptr = vm->vm_pptr;
  156.  
  157.       /* if we got our own malloc list already, it is save to call malloc here.
  158.          If not, the stuff done here is postponed to either vfork_resume, or
  159.          execve */
  160.       if (vm->vm_own_malloc)
  161.     {
  162.       /* inherit these global variables. */
  163.       mu->u_environ = (char ***) malloc (4);
  164.       * mu->u_environ = dupvec (* pu->u_environ);
  165.       mu->u_errno   = (int *) malloc (4);
  166.       *mu->u_errno = 0;
  167.     }
  168.       else
  169.     {
  170.       /* borrow the variables of the parent */
  171.       mu->u_environ = pu->u_environ;
  172.       mu->u_errno = pu->u_errno;
  173.       
  174.       /* tell malloc to use the parents malloc lists */
  175.       mu->u_mdp = pu->u_mdp;
  176.     }
  177.  
  178.       
  179.       /* and inherit several other things as well, upto not including u_md */
  180.       bcopy ((void *)& pu->u_signal[0], (void *)& mu->u_signal[0],
  181.          offsetof (struct user, u_md) - offsetof (struct user, u_signal[0]));
  182.  
  183.       /* some things have been copied that should be reset */      
  184.       mu->p_flag &= ~SFREEA4;
  185.       mu->p_xstat = 0;
  186.       bzero ((void *)& mu->u_ru, sizeof (struct rusage));
  187.       bzero ((void *)& mu->u_cru, sizeof (struct rusage));
  188.       bzero ((void *)& mu->u_timer[0], sizeof (struct itimerval)); /* just the REAL timer! */
  189.       syscall (SYS_gettimeofday, & mu->u_start, 0);
  190.       omask = vm->vm_rc;    /* signal mask to restore at the end */
  191.  
  192.       /* and adjust the open count of each of the copied filedescriptors */
  193.       for (fd = 0; fd < NOFILE; fd++)
  194.         if (mu->u_ofile[fd])
  195.           mu->u_ofile[fd]->f_count++;
  196.  
  197.       /* copying finished, allow other processes to vfork() as well ;-)) */
  198.       ix_unlock_base ();
  199.       
  200.       /* remember the message we have to reply when either _exit() or 
  201.        * execve() is called */
  202.       mu->p_vfork_msg = vm;
  203.       
  204.       vm->vm_rc = 0;
  205.  
  206.       mu->u_save_sp = (void *) get_sp ();
  207.  
  208.       /* we get here when the user does an _exit() 
  209.        * (so as well after execve() terminates !) */
  210.       if ((rc = _setjmp (mu->u_jmp_buf)))
  211.         {
  212.       struct death_msg *dm = 0;
  213.       int i;
  214.  
  215.       /* overkill? */
  216.       vfork_own_malloc ();
  217.  
  218.       /* reset `mu' in here, setjmp() might have clobbered it */
  219.       mu = &u;
  220.  
  221.       /* although this is done in CloseLibrary(), files should 
  222.          really be closed *before* a death-message is sent to
  223.          the parent. */
  224.       for (i = 0; i < NOFILE; i++) 
  225.         if (u.u_ofile[i]) syscall (SYS_close, i);
  226.  
  227.       /* free memory (look that most, best all memory is freed here, as
  228.              long as we're not inside Forbid. If memory is freed in CloseLibrary,
  229.          it may potentially have to wait for the memory semaphore in buddy-alloc.c,
  230.          thus breaking the Forbid! */
  231.       all_free ();
  232.  
  233.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  234.  
  235.       /* KPRINTF (("vforked: _exit in progress, rc = %ld.\n", rc)); */
  236.  
  237.       /* this whole thing only happens if our parent is still alive ! */
  238.       if (mu->p_pptr && mu->p_pptr != (struct Process *) 1)
  239.         {
  240.           rc --;
  241.  
  242.           /* KPRINTF (("vforked: parent alive, zombie-sig = %ld, vfork_msg = $%lx.\n",
  243.          pu->p_zombie_sig, mu->p_vfork_msg));*/
  244.  
  245.           pu = (struct user *) (mu->p_pptr->pr_Task.tc_TrapData);
  246.  
  247.           /* send the parent a death message with our return code */
  248.           dm = (struct death_msg *) kmalloc (sizeof (struct death_msg));
  249.  
  250.           Forbid ();
  251.           if (dm)
  252.         {
  253.           dm->dm_status = mu->p_xstat;
  254.           dm->dm_rusage = mu->u_ru;
  255.           ruadd (&dm->dm_rusage, (struct rusage *)&mu->u_cru);
  256.           dm->dm_child = (struct Process *) FindTask (0);
  257.           dm->dm_pgrp  = mu->p_pgrp;
  258.           KPRINTF (("vfork-exit: Adding child $%lx to $%lx\n", dm->dm_child, mu->p_pptr));
  259.           AddTail ((struct List *) &pu->p_zombies, (struct Node *) dm);
  260.         }
  261.  
  262.           _psignal ((struct Task *)mu->p_pptr, SIGCHLD);
  263.  
  264.           /* have to wakeup the parent `by hand' to make sure it gets
  265.              out of its sleep, since it might have SIGCHLD masked out or
  266.              ignored at the moment */
  267.           if (pu->p_stat == SSLEEP && pu->p_wchan == (caddr_t) pu)
  268.         ix_wakeup ((u_int)pu);
  269.  
  270.           if (mu->p_vfork_msg)
  271.             ReplyMsg ((struct Message *) mu->p_vfork_msg);
  272.  
  273.           /* KPRINTF (("vforked: unlinking from parent process chains\n"));*/
  274.           /* unlink us from the parents process chains */
  275.  
  276.           if (mu->p_ysptr)
  277.             {
  278.               struct user *yu = (struct user *) (mu->p_ysptr->pr_Task.tc_TrapData);
  279.               yu->p_osptr = mu->p_osptr;
  280.             }
  281.  
  282.           if (mu->p_osptr)
  283.             {
  284.              struct user *ou = (struct user *) (mu->p_osptr->pr_Task.tc_TrapData);
  285.              ou->p_ysptr = mu->p_ysptr;
  286.             }
  287.  
  288.           if (pu->p_cptr == me)
  289.             pu->p_cptr = mu->p_osptr;
  290.  
  291.           /* moved this code into the if-block, as it should only be 
  292.              executed if there really is a parent that might wait for the
  293.              child to terminate (ignore that `this can't happen', perhaps
  294.              it does... */
  295.  
  296.           /* this seems to be necessary for process synchronisation, or
  297.              else it is possible that the same process address is
  298.              reused before the parent noticed the death of this child,
  299.              and this would rather confuse it (or ksh at least ;-)) */
  300.           if (dm)
  301.             ix_sleep ((caddr_t)dm, "vfork-dm");
  302.         }
  303.       else
  304.         {
  305.           Forbid ();
  306.           KPRINTF (("vforked: couldn't send death_msg\n"));
  307.         }
  308.  
  309.       KPRINTF (("vforked: now closing library\n"));
  310.  
  311.       /* temporary `fix'.. there seem to be some packets arriving
  312.          too late.. */
  313.       Delay (100);
  314.  
  315.       CloseLibrary (ixb);
  316.  
  317.       KPRINTF (("vforked: falling off the edge of the world.\n"));
  318.       /* just fall off the edge of the world, this is a process */
  319.       return;
  320.         }
  321.  
  322.       syscall (SYS_sigsetmask, omask);
  323.  
  324.       KPRINTF (("vforked: jumping back\n"));
  325.  
  326.       /* jump into nevereverland ;-) */
  327.       vfork_longjmp (vm->vm_regs->jb, 0);
  328.       /* NOTREACHED */
  329.     }
  330.  
  331.   vm->vm_rc = ENOMEM; /* can't imagine any other reason why the OpenLib should fail */
  332.   ReplyMsg ((struct Message *) vm);
  333.   /* fall off the edge of the world ;-) */
  334. }
  335.  
  336.  
  337. /* This function is used by vfork_resume and execve. Perhaps it should be made
  338.    externally available? It causes the process to switch to its own malloc
  339.    list, and copies errno and environ into private space. */
  340. void
  341. vfork_own_malloc (void)
  342. {
  343.   /* use volatile here, or the compiler might do wrong `optimization' .. */
  344.   volatile struct user *p = &u;
  345.  
  346.   if (p->u_mdp != &p->u_md)
  347.     {
  348.       char **parent_environ = *p->u_environ;
  349.       
  350.       /* switch to our memory list (which is initialized by OpenLibrary) */
  351.       p->u_mdp = (void *)&p->u_md;
  352.       /* dupvec now uses malloc() on our list */
  353.       p->u_environ = (char ***) malloc (4);
  354.       *p->u_environ = dupvec (parent_environ);
  355.       p->u_errno = (int *) malloc (4);
  356.       *p->u_errno = 0;
  357.     }
  358. }
  359.  
  360.  
  361. asm ("
  362.     .globl _vfork
  363.     .globl _vfork2
  364.     .globl _vfork_resume
  365. _vfork:
  366.     | store a setjmp () compatible frame on the stack to pass to _vfork ()
  367.     lea    sp@(-18*4),sp        | _JBLEN (17) longs on the stack
  368.     pea    sp@
  369.     jbsr    _setjmp
  370.     lea    sp@(4),sp
  371.     | now patch sp and pc, since they differ
  372.     addl    #20*4,sp@(8)        | account for buffer space
  373.     movel    sp@(18*4),sp@(20)    | insert real PC (return addr on stack)
  374.     | tell _vfork *not* yet to switch to own malloc-list
  375.     pea    0
  376.     bsr    __vfork
  377.     lea    sp@(18*4 + 4),sp
  378.     rts
  379.  
  380. _vfork2:
  381.     | this is the vfork used in older versions of the library
  382.     lea    sp@(-18*4),sp        | _JBLEN (17) longs on the stack
  383.     pea    sp@
  384.     jbsr    _setjmp
  385.     lea    sp@(4),sp
  386.     | now patch sp and pc, since they differ
  387.     addl    #20*4,sp@(8)        | account for buffer space
  388.     movel    sp@(18*4),sp@(20)    | insert real PC (return addr on stack)
  389.     | tell _vfork to have the child run with own malloc-list.
  390.     pea    1
  391.     bsr    __vfork
  392.     lea    sp@(18*4 + 4),sp
  393.     rts
  394.  
  395.  
  396.     | the following is longjmp(), with the subtle difference that this
  397.     | thing doesn't insist in returning something non-zero... 
  398. _vfork_longjmp:
  399.     movel    sp@(4),a0    /* save area pointer */
  400.     tstl    a0@(8)        /* ensure non-zero SP */
  401.     jeq    Lbotch        /* oops! */
  402.     movel    sp@(8),d0    /* grab return value */
  403.     moveml    a0@(28),d2-d7/a2-a4/a6    /* restore non-scratch regs */
  404.     movel    a0,sp@-        /* let sigreturn */
  405.     jbsr    _sigreturn    /*   finish for us */
  406.  
  407. Lbotch:
  408.     jsr    _longjmperror
  409.     stop    #0
  410.  
  411. _vfork_resume:
  412.     pea    sp@        | pass the sp containing the return address
  413.     bsr    __vfork_resume
  414.     lea    sp@(4),sp
  415.     rts
  416. ");
  417.  
  418. /*
  419.  * this is an implementation extension to the `real' vfork2(). Normally you
  420.  * can only cause the parent to resume by calling _exit() or execve() from
  421.  * the child. Since I can't provide a real fork() on the Amiga, this function
  422.  * is a third possibility to make the parent resume. You have then two
  423.  * concurrent processes sharing the same frame and global data... Please be
  424.  * EXTREMELY careful what you may do and what not. vfork2() itself is a hack,
  425.  * this is an even greater one...
  426.  *
  427.  * DO NOT use this function in combination with vfork(), or you'll get a big
  428.  * memory leak. Only use it with vfork2().
  429.  */
  430.  
  431. /*
  432.  * don't make this function static, gcc doesn't notice that it's used from the
  433.  * assembly code above, so make sure it isn't optimized away...
  434.  */
  435. void
  436. _vfork_resume (u_int *copy_from_sp)
  437. {
  438.   struct vfork_msg **vm = &u.p_vfork_msg;
  439.  
  440.   if (*vm)
  441.     {
  442.       u_int *sp = (u_int *)u.u_save_sp;
  443.       u_int *copy_till_sp = (u_int *)get_sp ();
  444.  
  445.       /* be sure to switch to our memory list */
  446.       vfork_own_malloc ();
  447.  
  448.       /* copy the stack frame */
  449.       copy_from_sp++;
  450.       do
  451.     *--sp = *--copy_from_sp;
  452.       while (copy_from_sp > copy_till_sp);
  453.  
  454.       set_sp ((u_int) sp);
  455.  
  456.       ReplyMsg ((struct Message *) *vm);
  457.       *vm = 0;
  458.     }
  459. }
  460.  
  461. /*
  462.  * don't make this function static, gcc doesn't notice that it's used from the
  463.  * assembly code above, so make sure it isn't optimized away...
  464.  */
  465. int
  466. _vfork (int own_malloc, struct reg_parms rp)
  467. {
  468.   struct Process *me = (struct Process *) FindTask(0);
  469.   struct CommandLineInterface *CLI = (void *)(me->pr_CLI);
  470.   u_int stack_size = get_stack_size(me);
  471.   /* those *have* to be in registers to survive the stack deallocation */
  472.   register struct vfork_msg *vm asm ("a2");
  473.   register int omask asm ("d2");
  474.   register struct Process *child asm ("a3");
  475.   struct TagItem tags [] = {
  476.     { NP_Entry, (ULONG) launcher, },
  477.     { NP_Cli, (ULONG) (CLI ? -1 : 0), },    /* same thing we are */
  478.     { NP_Name, (ULONG) "vfork()'d process", },    /* to be overridden by execve() */
  479.     { NP_StackSize, stack_size, },        /* same size we use */
  480.     { TAG_END, 0, }
  481.   };
  482.   KPRINTF (("vfork: creating child with stacksize = $%lx[$%lx,$%lx]\n", stack_size, tags[3]));
  483.   vm = (struct vfork_msg *) kmalloc (sizeof (struct vfork_msg));
  484.   if (! vm)
  485.     {
  486.       errno = ENOMEM;
  487.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  488.       return -1;
  489.     }
  490.  
  491.   vm->vm_msg.mn_ReplyPort = u.u_sync_mp;
  492.   vm->vm_msg.mn_Node.ln_Type = NT_MESSAGE;
  493.   vm->vm_msg.mn_Length = sizeof (struct vfork_msg);
  494.   vm->vm_pptr = me;
  495.   vm->vm_own_malloc = own_malloc;
  496.   vm->vm_regs = & rp;
  497.  
  498.   /* we have to block all signals as long as the child uses our resources.
  499.    * but since the child needs to start with the signal mask BEFORE this
  500.    * general blocking, we have to pass it the old signal mask. This is a
  501.    * way to do it */
  502.   vm->vm_rc   = 
  503.   omask      = syscall (SYS_sigsetmask, ~0);
  504.  
  505.   /* save the passed frame in our user structure, since the child will
  506.      deallocate it from the stack when it `returns' to user code */
  507.   bcopy (&rp, &u.u_vfork_frame, sizeof (rp));
  508.  
  509.   KPRINTF (("vfork: creating child with stacksize = $%lx[$%lx,$%lx]\n", stack_size, tags[3]));
  510.   child = CreateNewProc (tags);
  511.  
  512.  
  513.   if (! child)
  514.     {
  515.       /* do I have to close input/output here? Or does the startup close
  516.          them no matter whether it succeeds or not ? */
  517.       kfree (vm);
  518.       syscall (SYS_sigsetmask, omask);
  519.       errno = EPROCLIM;
  520.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  521.       return -1;
  522.     }
  523.  
  524.   /* As soon as this message is dispatched, the child will `return' and 
  525.      deallocate the stack we're running on. So afterwards, *only* use
  526.      register variables and then longjmp () back.
  527.      Since we don't have a stack until after the longjmp(), temporarily
  528.      switch to our mini-stack */
  529.   set_sp ((u_int) &u.u_mini_stack[sizeof (u.u_mini_stack) / sizeof (long)]);
  530.  
  531.   vm->vm_self = child;
  532.   PutMsg (& child->pr_MsgPort, (struct Message *) vm);
  533.   /* wait until the child does execve() or _exit() */
  534.   WaitPort (u.u_sync_mp);
  535.   GetMsg (u.u_sync_mp);
  536.   syscall (SYS_sigsetmask, omask);
  537.  
  538.   if (vm->vm_rc)
  539.     {
  540.       errno = (int) vm->vm_rc;
  541.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  542.       child = (struct Process *) -1;
  543.     }
  544.       
  545.   /* this is the parent return, so we pass the id of the new child */
  546.   kfree (vm);
  547.   /* could use longjmp() here, but since we already *have* the local one.. */
  548.   vfork_longjmp (u.u_vfork_frame, (int) child);
  549.   return 0;  /* not reached */
  550. }
  551.  
  552. void ruadd(struct rusage *ru, struct rusage *ru2)
  553. {
  554.     register long *ip, *ip2;
  555.     register int i;
  556.  
  557.     timevaladd(&ru->ru_utime, &ru2->ru_utime);
  558.     timevaladd(&ru->ru_stime, &ru2->ru_stime);
  559.     if (ru->ru_maxrss < ru2->ru_maxrss)
  560.         ru->ru_maxrss = ru2->ru_maxrss;
  561.     ip = &ru->ru_first; ip2 = &ru2->ru_first;
  562.     for (i = &ru->ru_last - &ru->ru_first; i > 0; i--)
  563.         *ip++ += *ip2++;
  564. }
  565.  
  566.  
  567. /* This function is in desperate need of redesign !!!! */
  568.  
  569. int
  570. wait4 (int pid, int *status, int options, struct rusage *rusage)
  571. {
  572.   struct Process * me = (struct Process *) FindTask (0);
  573.   struct user * mu = (struct user *) me->pr_Task.tc_TrapData;
  574.  
  575.   for (;;)
  576.     {
  577.       int err = 0;
  578.       struct death_msg *dm, *ndm;
  579.       int got_node;
  580.  
  581.       got_node = 0;
  582.       Forbid ();
  583.       
  584.       for (dm  = (struct death_msg *) mu->p_zombies.mlh_Head;
  585.          (ndm = (struct death_msg *) dm->dm_node.mln_Succ);
  586.          dm  = ndm)
  587.       if (pid == -1 ||
  588.           (pid == 0 && dm->dm_pgrp == mu->p_pgrp) ||
  589.         (pid < -1 && dm->dm_pgrp == - pid) ||
  590.         (pid == (int) dm->dm_child))
  591.       {
  592.         got_node = 1;
  593.         Remove ((struct Node *) dm);
  594.         break;
  595.       }
  596.  
  597.       if (!got_node && !mu->p_cptr)
  598.     err = ECHILD;
  599.  
  600.       if (got_node)
  601.     /* Handle exited children first.  */
  602.         {
  603.       struct Process *child;
  604.  
  605.       KPRINTF (("wait4: unlinking child $%lx\n", dm->dm_child));
  606.       ix_wakeup ((u_int)dm);
  607.       Permit ();
  608.  
  609.           if (status)
  610.           {
  611.             *status = dm->dm_status;
  612.           }  
  613.           if (rusage)
  614.             *rusage = dm->dm_rusage;
  615.  
  616.       child = dm->dm_child;
  617.  
  618.       kfree (dm);
  619.  
  620.           return (int) child;
  621.     }
  622.       else
  623.     /* No child processes have died for now.
  624.        Do we have a traced child process to handle?  */
  625.     {
  626.       struct Process *p;
  627.       struct user *pu;
  628.  
  629.       KPRINTF(("wait4: checking traced children, pid=%lx, mu->p_cptr=%lx\n",
  630.            pid, mu->p_cptr));
  631.  
  632.       for (p = mu->p_cptr;
  633.            p;
  634.            p = ((struct user *) p->pr_Task.tc_TrapData)->p_osptr)
  635.         {
  636.           KPRINTF(("wait4: checking pid p=%lx\n", p));
  637.           if ((pu = p->pr_Task.tc_TrapData))
  638.             if (pid == -1
  639.             || ((int) p) == pid
  640.             || pu->p_pgrp == -pid
  641.             || (pid == 0
  642.                 && mu->p_pgrp == pu->p_pgrp))
  643.           {        
  644.             KPRINTF(("wait4: pu->p_stat=%lx, pu->flag=%lx\n",
  645.                 pu->p_stat, pu->p_flag));
  646.             if (pu->p_stat == SSTOP
  647.                 && (pu->p_flag & SWTED) == 0
  648.                 && (pu->p_flag & STRC || options & WUNTRACED))
  649.               {
  650.                 KPRINTF(("wait4: SSTOPed; p_xstat=0x%lx, W_STOPCODEd=0x%lx\n",
  651.                 pu->p_xstat, W_STOPCODE (pu->p_xstat)));
  652.                 pu->p_flag |= SWTED;
  653.                 if (status)
  654.               *status = W_STOPCODE (pu->p_xstat);
  655.                 KPRINTF(("wait4: status was set to %ld\n", *status));
  656.                 Permit ();
  657.                 return (int)p;
  658.               }
  659.           }
  660.         }
  661.     }
  662.  
  663.       if (!got_node && err)
  664.     {
  665.       Permit ();
  666.       errno = err;
  667.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  668.       return -1;
  669.     }
  670.  
  671.       if (options & WNOHANG)
  672.     {
  673.       Permit ();
  674.       return 0;
  675.     }
  676.  
  677.       KPRINTF (("wait4: waiting for SIGCHLD\n"));
  678.       ix_sleep ((caddr_t)mu, "wait4");
  679.       if (mu->p_sig)
  680.     err = EINTR;
  681.  
  682.       Permit ();
  683.       if (CURSIG (mu))
  684.         setrun ((struct Task *)me);
  685.     }
  686. }
  687.